package com.segi.view.calendar.numberpicker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.segi.view.R;
import com.segi.view.calendar.numberpicker.CustomNumberPickerView.OnValueChangeListener;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import cn.segi.framework.log.Logger;
import cn.segi.framework.util.StringUtils;
import cn.segi.framework.util.WindowDispaly;

/**
 * 自定义日历控件，目前支持 年月 2016-12 年 2016 两种格式
 * 年月日格式暂时不支持
 *
 * @author Administrator
 */
public class CustomDateTimePickerDialog extends Dialog implements View.OnClickListener {

    /**
     * 选择后的事件监听
     */
    private NumPickerListener mNumPickerListener;
    /**
     *
     */
    private CustomNumberPickerView mYearPickerView, mMonthPickerView, mDayPickerView, mHourPickerView, mMinutePickerView, mDatePickerView;
    /**
     * 默认年月日时分的时间格式
     */
    private PickerEnum mPickType = PickerEnum.YYYYMMDDHHMM;
    /**
     * 容器
     */
    private LinearLayout mContentLayout;
    /**
     * 记录时间值
     */
    private int mYear = 0;
    private int mMonth = 0;
    private int mDay = 0;
    private int mHour = 0;
    private int mMinute = 0;
    private String mInitTime;
    private String mFormatStr = "yyyy-MM-dd HH:mm:ss";
    /**
     * 标题名称
     */
    private String mTitleName;

    /**
     * @param context           上下文
     * @param theme             样式
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     * @param titleName         标题名称
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, int theme, PickerEnum pickType, NumPickerListener numPickerListener, String titleName, String initTime) {
        super(context, theme);
        mPickType = pickType;
        mNumPickerListener = numPickerListener;
        mTitleName = titleName;
        mInitTime = initTime;
    }

    /**
     * @param context           上下文
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     * @param titleName         标题名称
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, PickerEnum pickType, NumPickerListener numPickerListener, String titleName, String initTime) {
        this(context, R.style.CustomDialog, pickType, numPickerListener, titleName, initTime);
    }

    /**
     * 默认时间格式：PickerEnum.YYYYMMDDHHMM
     *
     * @param context           上下文
     * @param numPickerListener 选择监听
     * @param titleName         标题名称
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, NumPickerListener numPickerListener, String titleName, String initTime) {
        this(context, PickerEnum.YYYYMMDDHHMM, numPickerListener, titleName, initTime);
    }

    /**
     * 默认时间格式：PickerEnum.YYYYMMDDHHMM
     *
     * @param context           上下文
     * @param numPickerListener 选择监听
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, NumPickerListener numPickerListener, String initTime) {
        this(context, numPickerListener, null, initTime);
    }

    /**
     * @param context           上下文
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, PickerEnum pickType, NumPickerListener numPickerListener, String initTime) {
        this(context, R.style.CustomDialog, pickType, numPickerListener, null, initTime);
    }

    /**
     * @param context           上下文
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     */
    public CustomDateTimePickerDialog(Context context, PickerEnum pickType, NumPickerListener numPickerListener) {
        this(context, R.style.CustomDialog, pickType, numPickerListener, null, null);
    }

    /**
     * @param context           上下文
     * @param theme             样式
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     */
    public CustomDateTimePickerDialog(Context context, int theme, PickerEnum pickType, NumPickerListener numPickerListener) {
        this(context, theme, pickType, numPickerListener, null, null);
    }

    /**
     * @param context           上下文
     * @param theme             样式
     * @param pickType          日期格式{@link PickerEnum}
     * @param numPickerListener 选择监听
     * @param initTime          初始时间
     */
    public CustomDateTimePickerDialog(Context context, int theme, PickerEnum pickType, NumPickerListener numPickerListener, String initTime) {
        this(context, theme, pickType, numPickerListener);
        mInitTime = initTime;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_datetime_picker);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);

        if (!TextUtils.isEmpty(mTitleName)) {
            TextView titleTv = (TextView) findViewById(R.id.title_tv);
            titleTv.setText(mTitleName);
        }
        findViewById(R.id.ok_tv).setOnClickListener(this);
        mContentLayout = (LinearLayout) findViewById(R.id.content_ll);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mContentLayout.getLayoutParams();
        params.width = WindowDispaly.getRealWidth();
        mContentLayout.setLayoutParams(params);
        initPickerView();
    }

    /**
     * 初始化选择器
     */
    private void initPickerView() {
        if (PickerEnum.YYYYMMDDHHMM == mPickType) {
            mYearPickerView = new CustomNumberPickerView(getContext(), R.style.YearNumPickerTheme);
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            mMinutePickerView = new CustomNumberPickerView(getContext(), R.style.MinuteNumPickerTheme);
            setYearValueChangeListener();
            setMonthValueChangeListener();
            mFormatStr = "yyyy-MM-dd HH:mm";
        } else if (PickerEnum.YYYYMMDDHH == mPickType) {
            mYearPickerView = new CustomNumberPickerView(getContext(), R.style.YearNumPickerTheme);
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            setYearValueChangeListener();
            setMonthValueChangeListener();
            mFormatStr = "yyyy-MM-dd HH";
        } else if (PickerEnum.YYYYMMDD == mPickType) {
            mYearPickerView = new CustomNumberPickerView(getContext(), R.style.YearNumPickerTheme);
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            setYearValueChangeListener();
            setMonthValueChangeListener();
            mFormatStr = "yyyy-MM-dd";
        } else if (PickerEnum.YYYYMM == mPickType) {
            mYearPickerView = new CustomNumberPickerView(getContext(), R.style.YearNumPickerTheme);
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mFormatStr = "yyyy-MM";
        } else if (PickerEnum.MMDDHHMM == mPickType) {
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            mMinutePickerView = new CustomNumberPickerView(getContext(), R.style.MinuteNumPickerTheme);
            setMonthValueChangeListener();
            mFormatStr = "MM-dd HH:mm";
        } else if (PickerEnum.HHMM == mPickType) {
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            mMinutePickerView = new CustomNumberPickerView(getContext(), R.style.MinuteNumPickerTheme);
            mFormatStr = "HH:mm";
        } else if (PickerEnum.MMDD == mPickType) {
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            setMonthValueChangeListener();
            mFormatStr = "MM-dd";
        } else if (PickerEnum.MMDDHH == mPickType) {
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            setMonthValueChangeListener();
            mFormatStr = "MM-dd HH";
        } else if (PickerEnum.YYYY == mPickType) {
            mYearPickerView = new CustomNumberPickerView(getContext(), R.style.YearNumPickerTheme);
            mFormatStr = "yyyy";
        } else if (PickerEnum.MM == mPickType) {
            mMonthPickerView = new CustomNumberPickerView(getContext(), R.style.MonthNumPickerTheme);
            mFormatStr = "MM";
        } else if (PickerEnum.DD == mPickType) {
            mDayPickerView = new CustomNumberPickerView(getContext(), R.style.DayNumPickerTheme);
            mFormatStr = "dd";
        } else if (PickerEnum.DDHH == mPickType) {
            mDatePickerView = new CustomNumberPickerView(getContext(), R.style.DateNumPickerTheme);
            mHourPickerView = new CustomNumberPickerView(getContext(), R.style.HourNumPickerTheme);
            mFormatStr = "dd HH";
        }
        setPickerViewParamsAndValue();
    }

    /**
     * 设置选择器布局
     */
    private void setPickerViewParamsAndValue() {
        Calendar calendar;
        if (TextUtils.isEmpty(mInitTime) || TextUtils.isEmpty(mFormatStr)) {
            calendar = Calendar.getInstance(Locale.CHINA);
        } else {
            calendar = Calendar.getInstance(Locale.CHINA);
            final SimpleDateFormat format = new SimpleDateFormat(mFormatStr);
            try {
                calendar.setTime(format.parse(mInitTime));
            } catch (ParseException e) {
                e.printStackTrace();
                calendar = Calendar.getInstance(Locale.CHINA);
            }
        }
        if (null != calendar) {
            mYear = calendar.get(Calendar.YEAR);
            if (mYear > 2050 || mYear < 1950) {
                mYear = 2018;
            }
            mMonth = calendar.get(Calendar.MONTH) + 1;
            mDay = calendar.get(Calendar.DAY_OF_MONTH);
            mHour = calendar.get(Calendar.HOUR_OF_DAY);
            mMinute = calendar.get(Calendar.MINUTE);
            LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            itemParams.weight = 1;
            if (null != mYearPickerView) {
                mYearPickerView.setLayoutParams(itemParams);
                mYearPickerView.setMinValue(1950);
                mYearPickerView.setMaxValue(2050);
                mYearPickerView.setValue(mYear);
                mContentLayout.addView(mYearPickerView);
            }
            if (null != mMonthPickerView) {
                mMonthPickerView.setLayoutParams(itemParams);
                mMonthPickerView.setMinValue(1);
                mMonthPickerView.setMaxValue(12);
                mMonthPickerView.setValue(mMonth);
                mContentLayout.addView(mMonthPickerView);
            }
            if (null != mDayPickerView) {
                int maxDayNum = calendar.getActualMaximum(Calendar.DATE);
                refreshDayPickerView(maxDayNum);
                mDayPickerView.setLayoutParams(itemParams);
                mDayPickerView.setValue(mDay);
                mContentLayout.addView(mDayPickerView);
            }
            if (null != mDatePickerView) {
                mDatePickerView.setLayoutParams(itemParams);
                mDatePickerView.setMinValue(0);
                mDatePickerView.setMaxValue(30);
                if (mDay > 30 || mDay < 0) {
                    mDay = 0;
                }
                mDatePickerView.setValue(mDay);
                mContentLayout.addView(mDatePickerView);
//                TextView title = (TextView) findViewById(R.id.dayTitle);
//                title.setText("天");
//                title.setVisibility(View.VISIBLE);
            }
            if (null != mHourPickerView) {
                mHourPickerView.setLayoutParams(itemParams);
                mHourPickerView.setMinValue(0);
                mHourPickerView.setMaxValue(23);
                mHourPickerView.setValue(mHour);
                mContentLayout.addView(mHourPickerView);
            }
            if (null != mMinutePickerView) {
                mMinutePickerView.setLayoutParams(itemParams);
                mMinutePickerView.setMinValue(0);
                mMinutePickerView.setMaxValue(59);
                mMinutePickerView.setValue(mMinute);
                mContentLayout.addView(mMinutePickerView);
            }
        } else {
            dismiss();
        }
    }


    /**
     * 确定按钮的回调
     *
     * @author Administrator
     */
    public interface NumPickerListener {
        /**
         * 确定按钮的回调
         *
         * @param year
         * @param month
         * @param day
         * @param hour
         * @param minute
         */
        void timeSelectedCallBack(String year, String month, String day, String hour, String minute);
    }

    /**
     * 设置年份监听事件
     */
    private void setYearValueChangeListener() {
        if (null != mYearPickerView) {
            mYearPickerView.setOnValueChangedListener(new OnValueChangeListener() {

                @Override
                public void onValueChange(CustomNumberPickerView picker, int oldVal, int newVal) {
                    mYear = (int) StringUtils.getNumberFromString(mYearPickerView.getContentByCurrValue());
                    Calendar c = Calendar.getInstance(Locale.CHINA);
                    c.set(Calendar.DAY_OF_MONTH, 1);
                    c.set(Calendar.YEAR, mYear);
                    c.set(Calendar.MONTH, mMonth - 1);
                    int maxDayNum = c.getActualMaximum(Calendar.DATE);

                    if (mDayPickerView.getMaxValue() != maxDayNum) {
                        int curValue = (int) StringUtils.getNumberFromString(mDayPickerView.getContentByCurrValue());
                        refreshDayPickerView(maxDayNum);
                        mDayPickerView.setValue(curValue > maxDayNum ? maxDayNum : curValue);
                    }

                    Logger.d("onValueChange", mYearPickerView.getContentByCurrValue() + " " + maxDayNum);
                }
            });
        }
    }

    /**
     * 刷新日选择器
     *
     * @param maxDayNum
     */
    private void refreshDayPickerView(int maxDayNum) {
        String[] values = new String[maxDayNum];
        for (int i = 1; i <= maxDayNum; i++) {
            if (i <= 9) {
                values[i - 1] = "0" + Integer.toString(i) + "日";
            } else {
                values[i - 1] = Integer.toString(i) + "日";
            }
        }
        mDayPickerView.setDisplayedValues(values, true);
        mDayPickerView.setMinValue(1);
        mDayPickerView.setMaxValue(maxDayNum);
    }

    /**
     * 设置月份监听事件
     */
    private void setMonthValueChangeListener() {
        if (null != mMonthPickerView) {
            mMonthPickerView.setOnValueChangedListener(new OnValueChangeListener() {

                @Override
                public void onValueChange(CustomNumberPickerView picker, int oldVal,
                                          int newVal) {
                    mMonth = (int) StringUtils.getNumberFromString(mMonthPickerView.getContentByCurrValue());
                    Calendar c = Calendar.getInstance(Locale.CHINA);
                    c.set(Calendar.DAY_OF_MONTH, 1);
                    c.set(Calendar.YEAR, mYear);
                    c.set(Calendar.MONTH, mMonth - 1);
                    int maxDayNum = c.getActualMaximum(Calendar.DATE);

                    if (mDayPickerView.getMaxValue() != maxDayNum) {
                        String[] values = new String[maxDayNum];
                        for (int i = 1; i <= maxDayNum; i++) {
                            if (i <= 9) {
                                values[i - 1] = "0" + Integer.toString(i) + "日";
                            } else {
                                values[i - 1] = Integer.toString(i) + "日";
                            }
                        }
                        int curValue = (int) StringUtils.getNumberFromString(mDayPickerView.getContentByCurrValue());
                        mDayPickerView.setDisplayedValues(values, true);
                        mDayPickerView.setMaxValue(maxDayNum);
                        mDayPickerView.setValue(curValue > maxDayNum ? maxDayNum : curValue);
                    }
                    Logger.d("onValueChange", mMonthPickerView.getContentByCurrValue() + " " + maxDayNum);
                }
            });
        }
    }


    @Override
    public void onClick(View v) {
        dismiss();
        if (R.id.ok_tv == v.getId()) {
            if (null != mNumPickerListener) {
                String year = "";
                String month = "";
                String day = "";
                String hour = "";
                String minute = "";

                if (null != mYearPickerView) {
                    year = mYearPickerView.getContentByCurrValue();
                    year = StringUtils.getNumberStrFromString(year);
                } else {
                    year = Integer.toString(mYear);
                }
                if (null != mMonthPickerView) {
                    month = mMonthPickerView.getContentByCurrValue();
                    month = StringUtils.getNumberStrFromString(month);
                } else {
                    month = Integer.toString(mMonth);
                }
                if (null != mDayPickerView) {
                    day = mDayPickerView.getContentByCurrValue();
                    day = StringUtils.getNumberStrFromString(day);
                } else if (null != mDatePickerView) {
                    day = mDatePickerView.getContentByCurrValue();
                    day = StringUtils.getNumberStrFromString(day);
                } else {
                    day = Integer.toString(mDay);
                }
                if (null != mHourPickerView) {
                    hour = mHourPickerView.getContentByCurrValue();
                    hour = StringUtils.getNumberStrFromString(hour);
                } else {
                    hour = Integer.toString(mHour);
                }
                if (null != mMinutePickerView) {
                    minute = mMinutePickerView.getContentByCurrValue();
                    minute = StringUtils.getNumberStrFromString(minute);
                } else {
                    minute = Integer.toString(mMinute);
                }
                mNumPickerListener.timeSelectedCallBack(year, month, day, hour, minute);
            }
        }
    }

    /**
     * 设置年份
     *
     * @param year
     */
    public void setYear(int year) {
        if (null != mYearPickerView && mYearPickerView.getMaxValue() >= year) {
            mYearPickerView.setValue(year);
        }
    }

    /**
     * 设置月份
     *
     * @param month
     */
    public void setMonth(int month) {
        if (null != mMonthPickerView && mMonthPickerView.getMaxValue() >= month) {
            mMonthPickerView.setValue(month);
        }
    }

    /**
     * 设置天数
     *
     * @param day
     */
    public void setDay(int day) {
        if (null != mDayPickerView && mDayPickerView.getMaxValue() >= day) {
            mDayPickerView.setValue(day);
        }
    }

    /**
     * 设置小时
     *
     * @param hour
     */
    public void setHour(int hour) {
        if (null != mHourPickerView && mHourPickerView.getMaxValue() >= hour) {
            mHourPickerView.setValue(hour);
        }
    }

    /**
     * 设置分钟
     *
     * @param minute
     */
    public void setMinute(int minute) {
        if (null != mMinutePickerView && mMinutePickerView.getMaxValue() >= minute) {
            mMinutePickerView.setValue(minute);
        }
    }

}
